<template>
  <div class="tools-div">
    <el-button type="success" size="small" @click="addShow">添 加</el-button>
  </div>
  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
    <el-form label-width="120px">
      <el-form-item label="菜单标题">
        <el-input v-model="category.name" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="category.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">提交</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <el-table
    :data="list"
    style="width: 100%;margin-bottom: 20px"
    row-key="id"
    border
    default-expand-all
  >
    <el-table-column prop="name" label="分类名称" />
    <el-table-column prop="status" label="状态" #default="scope">
      {{ scope.row.status == 1 ? '正常' : '停用' }}
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column label="操作" align="center" width="280" #default="scope">
      <el-button type="success" size="small" @click="addShow(scope.row)">
        添加下级节点
      </el-button>
      <el-button type="primary" size="small" @click="editShow(scope.row)">
        修改
      </el-button>
      <el-button type="danger" size="small" @click="remove(scope.row.id)">
        删除
      </el-button>
    </el-table-column>
  </el-table>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {
  FindAllCategory,
  SaveCategory,
  UpdateCategoryById,
  RemoveCategoryById,
} from '@/api/category.js'
import { ElMessage, ElMessageBox } from 'element-plus'

//页面表单数据
const defaultForm = {
  id: '',
  parentId: 0,
  name: '',
  status: 1,
  postNum: 1,
}
// 定义list属性模型
const list = ref([])

// 表单响应式数据模型对象
const category = ref(defaultForm)

// 定义添加表单菜单表单相关数据模型
const dialogTitle = ref('添加')
const dialogVisible = ref(false)

//=======================添加和修改功能====================
//进入添加
const addShow = row => {
  category.value = {}
  dialogVisible.value = true
  if (!row.id) {
    dialogTitle.value = '添加'
  } else {
    dialogTitle.value = '添加下级节点'
    category.value.parentId = row.id
  }
}

//进入修改
const editShow = row => {
  category.value = row
  dialogVisible.value = true
}

//提交保存与修改
const saveOrUpdate = () => {
  if (!category.value.id) {
    if (!category.value.parentId) {
      category.value.parentId = 0
    }
    saveData()
  } else {
    updateData()
  }
}

// 修改
const updateData = async () => {
  await UpdateCategoryById(category.value)
  dialogVisible.value = false
  ElMessage.success('操作成功')
  fetchData()
}

// 新增
const saveData = async () => {
  await SaveCategory(category.value)
  dialogVisible.value = false
  ElMessage.success('操作成功')
  fetchData()
}

//=======================删除功能====================
const remove = async id => {
  console.log('removeDataById:' + id)
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code, message } = await RemoveCategoryById(id)
    if (code === 200) {
      ElMessage.success('删除成功')
      fetchData()
    } else {
      ElMessage.error(message)
    }
  })
}

/////////////////////////////////分类页表
// 页面初始化完毕以后请求后端接口查询数据
onMounted(() => {
  fetchData()
})

// 加载数据的方法
const fetchData = async () => {
  // 向后端发送请求获取数据
  const { code, data, message } = await FindAllCategory()
  list.value = data
  //console.log(data)
}
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
</style>
